package net.zjitc.dao;

import net.zjitc.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;


import java.io.IOException;
import java.io.InputStream;

/**
 * Created by Administrator on 2017/12/20.
 */
public class UserDaoImpl implements UserDao {

    public User findById(int id) throws IOException {
        /**
         * 1.读文件，生成工厂
         * 2.工厂生成会话
         * 3.会话执行语句
         * 4.关闭会话
         *
         * */
        InputStream in= Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession=factory.openSession();
        User user=sqlSession.selectOne("test.findUserById",id);
        sqlSession.close();
        return user;
    }
}
